package com.neusoft.weekreport.component.bizService.impl;

import com.neusoft.weekreport.component.bizService.ProductBizService;
import com.neusoft.weekreport.component.entities.DcProduct;
import com.neusoft.weekreport.component.entities.DcProductExample;
import com.neusoft.weekreport.component.req.GetAddProductInfoReq;
import com.neusoft.weekreport.component.req.GetProductInfoListReq;
import com.neusoft.weekreport.component.resp.GetAddProductInfoResp;
import com.neusoft.weekreport.component.resp.GetProductInfoByIdResp;
import com.neusoft.weekreport.component.resp.GetProductInfoListResp;
import com.neusoft.weekreport.mapper.DcProductMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class ProductBizServiceImpl  implements ProductBizService {

    @Resource
    DcProductMapper dcProductMapper;

    //查询产品列表
    @Override
    public GetProductInfoListResp getProductInfoList(GetProductInfoListReq req) throws Exception {
        GetProductInfoListResp resp = new GetProductInfoListResp();
        DcProductExample dcProductExample = new DcProductExample();
        DcProductExample.Criteria criteria = dcProductExample.createCriteria();
        if(!req.getId().isEmpty()){
            criteria.andIdEqualTo(req.getId());
        }
        dcProductExample.setOrderByClause("order_Num");
        List<DcProduct> dcProductList = dcProductMapper.selectByExample(dcProductExample);
        resp.setDcProductList(dcProductList);
        return resp;
    }

    //添加产品信息
    @Override
    public GetAddProductInfoResp addProductInfo(GetAddProductInfoReq req) throws Exception {
        GetAddProductInfoResp resp = new GetAddProductInfoResp();
        DcProduct dcProduct = new DcProduct();
        String uuid = UUID.randomUUID().toString();
        dcProduct.setId(uuid);
        dcProduct.setProductName(req.getProductName());
        dcProduct.setSummary(req.getSummary());
        dcProduct.setParentId(req.getParentId());
        dcProduct.setEnable(req.getEnable());
        dcProduct.setOrderNum(req.getOrderNum());
        dcProduct.setUserId(req.getUserId());
        dcProduct.setCreateBy(sysDateTime());
        dcProduct.setUpdateDate(sysDateTime());
        dcProductMapper.insert(dcProduct);
        resp.setId(uuid);
        return resp;
    }

    //修改产品信息
    @Override
    public void modifyProductInfo(GetAddProductInfoReq req) throws Exception{
        DcProduct dcProduct  = new DcProduct();
        dcProduct.setId(req.getId());
        dcProduct.setProductName(req.getProductName());
        dcProduct.setSummary(req.getSummary());
        dcProduct.setEnable(req.getEnable());
        dcProduct.setOrderNum(req.getOrderNum());
        dcProduct.setUserId(req.getUserId());
        dcProduct.setUpdateDate(sysDateTime());
        dcProductMapper.updateByPrimaryKeySelective(dcProduct);
    }
    //删除产品信息
    @Override
    public void deleteProductInfo(GetAddProductInfoReq req) throws Exception{
        dcProductMapper.deleteByPrimaryKey(req.getId());
    }
    //查看产品信息
    @Override
    public GetProductInfoByIdResp getProductInfoById(GetAddProductInfoReq req) throws Exception{
        GetProductInfoByIdResp resp = new GetProductInfoByIdResp();
        DcProduct dcProduct = dcProductMapper.selectByPrimaryKey(req.getId());
        resp.setDcProduct(dcProduct);
        return resp;
    }
    //改变产品状态
    @Override
    public void changeProductStatus(GetAddProductInfoReq req) throws Exception {
        DcProduct dcProduct = new DcProduct();
        dcProduct.setId(req.getId());
        if(req.getEnable().equals(1)){
            dcProduct.setEnable(0);
        }
        if (req.getEnable().equals(0)){
            dcProduct.setEnable(1);
        }
        dcProduct.setUpdateDate(sysDateTime());
        dcProductMapper.updateByPrimaryKeySelective(dcProduct);
    }

    private String sysDateTime(){
        Date date = new Date();
        DateFormat dataFormat = new SimpleDateFormat("yyy-MM-dd hh:mm:ss");
        String createDate = dataFormat.format(date);
        return createDate;
    }

    //查询有效产品
    @Override
    public GetProductInfoListResp getValidProductList(GetProductInfoListReq req) throws Exception {
        GetProductInfoListResp resp = new GetProductInfoListResp();
        DcProductExample dcProductExample = new DcProductExample();
        DcProductExample.Criteria criteria = dcProductExample.createCriteria();
        criteria.andEnableEqualTo(1);
        dcProductExample.setOrderByClause("order_Num");
        List<DcProduct> dcProductList = dcProductMapper.selectByExample(dcProductExample);
        resp.setDcProductList(dcProductList);
        return resp;
    }
}
